﻿@model dynamic

@{
	ViewBag.Title = "Hue";
	Layout = "~/Views/Shared/SiteLayout.cshtml";
}
<h2>title</h2>
<div class="row">
	<div class="well col-lg-6 col-lg-offset-3" >
		<div id="hueInfo"></div>
	</div>
</div>
<div class="row">
	<div class="well col-lg-6 col-lg-offset-3">
		<div id="info" class="btn btn-default">Info</div>
		<div id="connect" class="btn btn-default">On</div>
		<div id="on" class="btn btn-default">Off</div>
		<div id="off" class="btn btn-default">Off</div>
	</div>
</div>

<script type="text/javascript">

	var localUrl = "http://172.22.22.165/api";

	$('#info').click(function () {
		$.ajax({
			dataType: "json",
			url: "/Hue/GetInfo",
			success: function (result) {
				console.log(result);
				localUrl = "http://" + result.IP + "/api";
			}
		});
	});
	$('#connect').click(function () {
		$.ajax(
			{
				url: localUrl,
				type: "POST",
				data: JSON.stringify({
					devicetype: "test user",
					username: "newdeveloper"
				}),
				dataType: 'application/json',
				success: function (msg) {
					$('#hueInfo').text(msg);
					console.log(msg);
				},
				error: function (msg) {
					console.log(msg);
				}
			}
		);


	});
	$('#on').click(function () {
		$.ajax({
			url: localUrl +'/newdeveloper/lights/1/state',
			type: 'PUT',
			data : JSON.stringify({
				on : true
			}),
			success: function (result) {
				console.log('light on');
			}
		});
	});
	$('#off').click(function () {
		$.ajax({
			url: localUrl + '/newdeveloper/lights/1/state',
			type: 'PUT',
			data: JSON.stringify({
				on: false
			}),
			success: function (result) {
				console.log('light off');
			}
		});
	});
</script>
